package com.azure.code.array;

import java.util.HashMap;
import java.util.Map;

public class TwoSum_1 {

    // 暴力破解
    public int[] twoSum(int[] nums, int target) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if(nums[i]+nums[j]==target){
                    return new int[]{i,j};
                }
            }
        }
        return new int[0];
    }

    public int[] twoSum_2(int[] nums, int target) {
        int n = nums.length;
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
           if(map.containsKey(target-nums[i])){
               return new int[]{i,map.get(target-nums[i])};
           }
           map.put(nums[i],i);
        }
        return new int[0];
    }
}
